package cn.com.lyj6851.modules.mp.service.impl;


import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.exception.EntityExistException;
import cn.com.lyj6851.modules.mp.domain.YdyWechatReply;
import cn.com.lyj6851.modules.mp.service.YdyWechatReplyService;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatReplyDto;
import cn.com.lyj6851.modules.mp.service.dto.YdyWechatReplyQueryCriteria;
import cn.com.lyj6851.modules.mp.service.mapper.WechatReplyMapper;
import cn.com.lyj6851.utils.FileUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxWechatReply")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YdyWechatReplyServiceImpl extends BaseServiceImpl<WechatReplyMapper, YdyWechatReply>  implements YdyWechatReplyService {
    private final IGenerator generator;

    @Override
    public Map<String, Object> queryAll(YdyWechatReplyQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YdyWechatReply> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YdyWechatReplyDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<YdyWechatReply> queryAll(YdyWechatReplyQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YdyWechatReply.class,criteria));
    }

    @Override
    public void download(List<YdyWechatReplyDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YdyWechatReplyDto ydyWechatReply:all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("关键字", ydyWechatReply.getKey());
            map.put("回复类型", ydyWechatReply.getType());
            map.put("回复数据", ydyWechatReply.getData());
            map.put("0=不可用  1 =可用", ydyWechatReply.getStatus());
            map.put("是否隐藏", ydyWechatReply.getHide());
            list.add(map);
        }
        FileUtil.downloadExcel(list,response);
    }

    @Override
    public YdyWechatReply isExist(String key) {
        YdyWechatReply ydyWechatReply = getOne(Wrappers.<YdyWechatReply>lambdaQuery()
                    .eq(YdyWechatReply::getKey,key));
        return ydyWechatReply;
    }

    /**
     * 创建
     * @param ydyWechatReply /
     */
    @Override
    public void create(YdyWechatReply ydyWechatReply) {
        if(isExist(ydyWechatReply.getKey())!=null){
            throw new EntityExistException(YdyWechatReply.class,"key",ydyWechatReply.getKey());
        }
        save(ydyWechatReply);
    }

    /**
     * 更新
     * @param ydyWechatReply /
     */
    @Override
    public void upDate(YdyWechatReply ydyWechatReply) {
        YdyWechatReply ydyWechatReply1 = getById(ydyWechatReply.getId());
        YdyWechatReply ydyWechatReply2 = isExist(ydyWechatReply.getKey());
        if(ydyWechatReply2 !=null && !ydyWechatReply1.getId().equals(ydyWechatReply2.getId())){
            throw new EntityExistException(YdyWechatReply.class,"key",ydyWechatReply.getKey());
        }
        ydyWechatReply1.copy(ydyWechatReply);
        saveOrUpdate(ydyWechatReply1);
    }
}
